﻿

namespace Mulala.Cavan.Presentation.Mvc.Controller
{
    using System;
    using Mulala.Cavan.Application.MainBoundedContext.InformationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.InformationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Presentation.Mvc.ViewModel;
    using Mulala.Cavan.Presentation.Seedwork;
    using System.Web.Mvc;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Domain.Seedwork;
    using System.Collections.Generic;
    using System.Linq;

    public class NoteController : EntityController<Note, NoteSearch, INoteViewModel, INoteAppService>, INoteController
    {
        public override string ControllerName
        {
            get { return ControllerConst.NOTE; }
        }

        public override bool UseDenied
        {
            get { return true; }
        }

        public NoteController()
        {
            this.Model = ModelFactory.CreateModel<INoteViewModel>();
        }

        [ActionName("send")]
        [HttpGet]
        public ActionResult AddNote(int? id)
        {
            if (id.HasValue)
            {
                Note noteToReply = this.AppService.GetById(id.Value);
                this.Model.NoteToReply = noteToReply;
            }
            return View(this.Model);
        }

        [ActionName("send")]
        [HttpPost]
        public JsonResult AddNote(NoteViewModel noteViewModel)
        {
            BoolGenericResult<Note> result = new BoolGenericResult<Note>();
            noteViewModel.Item.SenderUserAccount = UserAccountFactory.CreateUserAccount(this.Model.MasterModel.CurrentUserModel.UserAccountID, this.Model.MasterModel.CurrentUserModel.UserRole);
            if (noteViewModel.NoteToReply.IsNotNull())
            {
                result = this.AppService.AddNoteReply(noteViewModel.NoteToReply, noteViewModel.Item);
            }
            else
            {
                result = this.AppService.Add(noteViewModel.Item);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName("check")]
        public JsonResult UpdateNoteReceiverNewStatus(int id)
        {
            return Json("");
        }

        [ActionName("notesfrom")]
        public ActionResult GetNotesByReceiver()
        {
            INotesViewModel notesViewModel = ModelFactory.CreateModel<INotesViewModel>();
            IList<Note> notes = this.AppService.GetNotesByReceiver(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            notesViewModel.Notes = NoteViewModelConverter.Convert(notes);

            if (this.Model.MasterModel.CurrentUserModel.UserRole == SecurityIdentifier.Role.Corporation)
            {
                return View("~/Views/Note/corporationnotes.cshtml", notesViewModel);
            }
            else if (this.Model.MasterModel.CurrentUserModel.UserRole == SecurityIdentifier.Role.Nanny)
            {
                return View("~/Views/Note/nannynotes.cshtml", notesViewModel);
            }
            else if (this.Model.MasterModel.CurrentUserModel.UserRole == SecurityIdentifier.Role.Proprietor)
            {
                return View("~/Views/Note/proprietornotes.cshtml", notesViewModel);
            }
            return View("~/Views/Error/Error.cshtml", notesViewModel);
        }

        [ActionName("notesto")]
        public ActionResult GetNotes()
        {
            INotesViewModel notesViewModel = ModelFactory.CreateModel<INotesViewModel>();
            NoteSearch noteSearch = NoteSearchFactory.CreateNoteSearch(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            IList<Note> notes = this.AppService.GetBySearch(noteSearch).ToList();
            notesViewModel.Notes = NoteViewModelConverter.Convert(notes);

            if (this.Model.MasterModel.CurrentUserModel.UserRole == SecurityIdentifier.Role.Corporation)
            {
                return View("~/Views/Note/corporationnotesto.cshtml", notesViewModel);
            }
            else if (this.Model.MasterModel.CurrentUserModel.UserRole == SecurityIdentifier.Role.Nanny)
            {
                return View("~/Views/Note/nannynotesto.cshtml", notesViewModel);
            }
            else if (this.Model.MasterModel.CurrentUserModel.UserRole == SecurityIdentifier.Role.Proprietor)
            {
                return View("~/Views/Note/proprietornotesto.cshtml", notesViewModel);
            }
            return View("~/Views/Error/Error.cshtml", notesViewModel);
        }
    }
}
